﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Office.Interop.Excel;

namespace AirChina
{
    public partial class Form1 : Form
    {

        int currentPage = 1;

        AirChinaService acs = new AirChinaService();
        
        public Form1()
        {
            InitializeComponent();
            try
            {
                InitInternation();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message.ToString());
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void InitInternation()
        {
            //listBox1.Items.Clear();
            List<string> lists = new List<string>();
            string html = acs.GetProductsPage(currentPage.ToString());
            int page = acs.GetPageNumbers(html);
            List<DetailModel> detailList = new List<DetailModel>(); ;
            for (int i = 1; i <= page; i ++ )
            {
                html = acs.GetProductsPage(i.ToString());
                lists = acs.GetProduct(html);
                foreach (var item in lists)
                {
                    listBox1.Items.Add(item);
                    string[] arr = item.Split('-');
                    string id = arr[1];
                    string city = arr[2];
                    string str = acs.GetDetailPage(id, city);
                    detailList.AddRange(acs.DataAnalysis(str));
                }
            }
            dataGridView1.DataSource = detailList;
        }

        
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0)
            {
                MessageBox.Show("没有数据");
            }

            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            excel.Application.Workbooks.Add(true);
            excel.Visible = true;
            //生成字段名称      
            for (int i = 0; i < dataGridView1.ColumnCount; i++)
            {
                excel.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
            }
            //填充数据      
            for (int i = 0; i < dataGridView1.RowCount - 1; i++)
            {
                for (int j = 0; j < dataGridView1.ColumnCount; j++)
                {
                    if (dataGridView1[j, i].ValueType == typeof(string))
                    {
                        excel.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value;
                    }
                    else
                    {
                        excel.Cells[i + 2, j + 1] = dataGridView1[j, i].Value;
                    }
                }
            }
            MessageBox.Show("导出成功"); 
        }

        /// <summary>
        /// 点击listbox里面的item触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string item = listBox1.SelectedItem.ToString();
            string[] arr = item.Split('-');
            string id = arr[1];
            string city = arr[2];
            string str = acs.GetDetailPage(id, city);
            List<DetailModel> list = acs.DataAnalysis(str);
            dataGridView1.DataSource = list;
        }
    }
}
